#include <stdio.h>
#include <unistd.h>

int global_val = 100;

int main()
{
    // 创建子进程
    pid_t id = fork();
    
    if(id < 0)
    {
        perror("fork fail");
        return 0;
    }
    else if(id  == 0)
    {
        int count = 0;
        // child process
        while(1)
        {
            printf("我是子进程, pid:%d, ppid:%d, global_val:%d, &global_val:%p\n", getpid(), getppid(), global_val, &global_val);
            sleep(1);;

            ++count;

            if(count == 5)
            {
                global_val = 500;
            }
        }
    }
    else
    {
        // parent process
        while(2)
        {
            printf("我是父进程, pid:%d, ppid:%d, global_val:%d, &global_val:%p\n", getpid(), getppid(), global_val, &global_val);
            sleep(2);
        }   
    }


    return 0;
}
